package com.linkai.onlinetool.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@RestController
public class Md5Controller {

    /**
    * @Description: 不带盐的MD5加密
    * @Param: [inputPassword]
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/9
    */
    @GetMapping("/md5")
//    @CrossOrigin(value = "http://101.132.147.111", allowCredentials = "true")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String MD5WithoutSalt(@RequestParam("text") String text) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] result = md.digest(text.getBytes());
            // 下面两步是将字节数组转换成为对应的16进制字符串
            BigInteger bigInteger = new BigInteger(1, result);
            return bigInteger.toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            System.out.println("加密失败");
            return text;
        }
    }
}
